<template>
  <div>
    <el-card class="box-card">
      <el-form ref="form" :model="queryConditions" label-width="80px">
        <!-- 使用el-row和el-col实现水平布局 -->
        <el-row :gutter="20">
          <el-col :span="5.5">
            <el-form-item label="角色名">
              <el-input
                  size="small"
                  v-model="queryConditions.roleName"
                  placeholder="请输入用户名"
                  prefix-icon="el-icon-user">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" size="small" @click="handleQuery">查询</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" size="small" @click="resetConditions">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card>
      <!--批量删除按钮 批量删除选中数据-->
      <el-button type="danger" size="small" @click="handleDeleteSelected">批量删除</el-button>
      <!--添加用户按钮-->
      <el-button type="primary" size="small" @click="handleAdd">添加用户</el-button>
      <el-table
          ref="multipleTable"
          :data="roleList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
        >
        </el-table-column>
        <el-table-column prop="id" label="编号" width="180"/>
        <el-table-column prop="roleName" label="角色名" width="180"/>
        <el-table-column label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          background
          class="pagination-margin"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.queryConditions.pageNum"
          :page-sizes="[8, 6, 3, 1]"
          :page-size="this.queryConditions.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total">
      </el-pagination>
    </el-card>

    <!--添加用户弹窗-->
    <el-dialog
        :title=title
        :visible.sync="dialogFormVisible"
        width="30%"
        :close-on-click-modal="false">
      <el-form :model="roleForm" :rules="rules" ref="userForm">
        <el-form-item label="角色" prop="roleName">
          <el-input v-model="roleForm.roleName"></el-input>
        </el-form-item>

        <el-form-item label="权限" prop="permIds">
          <br>
          <!-- 全选功能 -->
          <el-checkbox @change="handleCheckAllChange">全选</el-checkbox>
          <!-- 垂直布局的权限列表 -->
          <el-checkbox-group v-model="roleForm.permIds">
            <el-checkbox label="1">用户查询</el-checkbox>
            <el-checkbox label="2">用户添加</el-checkbox>
            <el-checkbox label="3">用户更新</el-checkbox>
            <el-checkbox label="4">用户删除</el-checkbox>
            <el-checkbox label="5">角色查询</el-checkbox>
            <el-checkbox label="6">角色添加</el-checkbox>
            <el-checkbox label="7">角色更新</el-checkbox>
            <el-checkbox label="8">角色删除</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "",
  data() {
    return {
      queryConditions: {
        roleName: "",
        pageNum: 1,
        pageSize: 8
      },
      roleList: [],
      total: null,
      ids: [],
      permIds: [],
      title: null,
      roleForm: {
        id: null,
        roleName: "",
        permIds: []
      },
      dialogFormVisible: false,
      rules: {
        roleName: [
          {required: true, message: '请输入角色名', trigger: 'blur'}
        ],
        permIds: [
          {required: true, message: '请选择权限', trigger: 'change'}
        ]
      }
    }
  },
  methods: {
    findAll() {
      axios.get("/role/list", {
        params: this.queryConditions
      }).then(res => {
        if (res.code == '40003') {
          this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => { // 点击确定
            this.$router.push("/")
          }).catch(() => { // 点击取消

          });
        } else {
          console.log(res)
          if (res.code === 200) {
            this.roleList = res.data.list
            this.total = res.data.total
            console.log(this.roleList)
            console.log(this.total)
          }
        }
      }).catch(error => {
        console.log(error);
      })
    },
    handleQuery() {
      this.queryConditions.pageNum = 1
      this.findAll()
    },
    resetConditions() {
      this.queryConditions.roleName = ""
      this.queryConditions.pageNum = 1
      this.handleQuery()
    },
    handleSizeChange(val) {
      this.queryConditions.pageSize = val
      this.findAll()
    },
    handleCurrentChange(val) {
      this.queryConditions.pageNum = val
      this.findAll()
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除编号为' + row.id + '的用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //删除成功后,重新查询全部
        axios.get("/role/delete/" + row.id).then(res => {
          if (res.code == '40003') {
            this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => { // 点击确定
              this.$router.push("/")
            }).catch(() => { // 点击取消

            });
          } else {
            console.log(res);
            if (res.code === 200) {
              this.$message.success(res.msg);
              this.handleQuery()
            } else {
              this.$message.error(res.msg);
            }
          }
        }).catch(error => {
          console.log(error);
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    //批量删除选中数据
    handleDeleteSelected() {
      //判断是否选中了数据
      if (this.ids.length === 0) {
        this.$message.info('请选择要删除的数据');
        return;
      }
      this.$confirm('此操作将永久删除编号为' + this.ids + '的用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //发送请求删除选中的行
        axios.get("role/deleteBatch/" + this.ids).then(res => {
          if (res.code == '40003') {
            this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => { // 点击确定
              this.$router.push("/")
            }).catch(() => { // 点击取消

            });
          } else {
            console.log(res);
            if (res.code === 200) {
              this.$message.success(res.msg);
              this.handleQuery();
            } else {
              this.$message.error(res.msg);
            }
          }
        }).catch(error => {
          console.log(error);
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    //选择数据改变时触发
    handleSelectionChange(val) {
      this.ids = []
      //将选中的行的id添加到ids数组中
      val.forEach(item => {
        this.ids.push(item.id)
      })
    },
    handleEdit(row) {
      console.log(row)
      this.roleForm.id = row.id
      this.roleForm.roleName = row.roleName

      axios.get("/role/permIds/" + row.id).then(res => {
        if (res.code == '40003') {
          this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => { // 点击确定
            this.$router.push("/")
          }).catch(() => { // 点击取消

          });
        } else {
          console.log(res);
          if (res.code === 200) {
            this.$message.success(res.msg);
            this.roleForm.permIds = res.data.map(id => String(id));
            console.log(this.roleForm)
          } else {
            this.$message.error(res.msg);
          }
        }
      }).catch(error => {
        console.log(error);
      })
      this.title = '编辑角色'
      this.dialogFormVisible = true

    },
    handleAdd() {
      this.roleForm = {
        roleName: "",
        permIds: []
      }
      this.title = '添加角色'
      this.dialogFormVisible = true

    },
    handleCheckAllChange(checked) {
      // 全选时选中所有权限
      if (checked) {
        this.roleForm.permIds = ['1', '2', '3', '4', '5', '6', '7', '8'];
      } else {
        // 取消全选时清空权限
        this.roleForm.permIds = [];
      }
    },
    submitForm() {
      this.$refs.userForm.validate(valid => {
        if (valid) {
          if (this.title === '编辑角色'){
            axios.post('role/update', this.roleForm).then(res => {
              if (res.code == '40003') {
                this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => { // 点击确定
                  this.$router.push("/")
                }).catch(() => { // 点击取消

                });
              } else {
                if (res.code === 200) {
                  this.$message.success(res.msg);
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg);
                }
              }
            })
          }else {
            axios.post('role/add', this.roleForm).then(res => {
              if (res.code == '40003') {
                this.$confirm('用户状态过时,请重新登录. 点击取消,留在当前页.点击确定跳转登录页', '系统提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => { // 点击确定
                  this.$router.push("/")
                }).catch(() => { // 点击取消

                });
              } else {
                if (res.code === 200) {
                  this.$message.success(res.msg);
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg);
                }
              }
            })
          }
          // 发送请求逻辑...
          this.dialogFormVisible = false;
        }
      });
    }

  }
  ,
  created() {
    this.findAll()
  }
}
</script>

<style scoped>
.box-card {
  width: 100%;
  height: 70px;
  margin-bottom: 15px; /* 添加这行来增加卡片间距 */
}
</style>